package com.monkeygroup.mygoplus.view.home.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.TextView;

/**
 * Created by LCW on 2017/6/12.
 */

public class CheckOverSizeTextView extends TextView {

    private boolean isOverSize,overSize = false;
    private OnOverSizeChangedListener changedListener;

    public CheckOverSizeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public CheckOverSizeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CheckOverSizeTextView(Context context) {
        super(context);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (changedListener != null) {
            changedListener.onChanged(checkOverLine());
        }
    }

    private void init() {
        // invalidate when layout end
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                if (changedListener != null) {
                    changedListener.onChanged(checkOverLine());
                }
            }
        });
    }

    /**
     * <p>
     * <span style="color: purple;">
     * <em>check if the text content has ellipsis </em></span>
     * </p>
     *
     * @return if the text content over maxlines
     */
    public boolean checkOverLine() {
        int maxLine = getMaxLines();
        try {
            Layout mLayout = this.getLayout();
            if (mLayout == null)
                return false;
            int mmaxLine = mLayout.getLineCount();

            isOverSize = mmaxLine-maxLine> 0 ? true : false;
            if(isOverSize){
                overSize=true;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return isOverSize;

        //或者按照如下方法取得已经省略得行数（用此方法必须再xml文件加上 android:ellipsize="end"）
        /*extView textView = (TextView) findViewById(R.id.txt_course_introduce);
        Layout l=textView.getLayout();
        if(l!=null){
            int lines =l.getLineCount();
            LogUtils.d(TAG, "lines = " + lines);
            if(lines>0){
                if(l.getEllipsisCount(lines-1)>0){
                    LogUtils.d(TAG, "Text is ellipsized");
                }
            }
        }else {
            LogUtils.d(TAG,"Layout is null");
        }*/
    }

    public boolean isOverSize() {
        return overSize;
    }

    public void displayAll() {
        setMaxLines(Integer.MAX_VALUE);
        setEllipsize(null);
        overSize=false;
    }

    public void hide(int maxlines) {
        setEllipsize(TextUtils.TruncateAt.END);
        setMaxLines(maxlines);
        overSize=true;
    }

    // set a listener for callback
    public OnOverSizeChangedListener getChangedListener() {
        return changedListener;
    }

    public void setOnOverLineChangedListener(OnOverSizeChangedListener changedListener) {
        this.changedListener = changedListener;
    }

    public interface OnOverSizeChangedListener {
        /**
         * <span style="color:purple">when invalide,the method will be called
         * and tell you whether the content text is over size
         *
         * @param isOverSize
         *            whether content text is over size
         */
        public void onChanged(boolean isOverSize);
    };
}